module Fabriq
  module Config
    extend self
    extend Fabriq::Logging

    attr_accessor :display, :runtime_env, :plugin_port, :skype_name, :adapter

    def plugin_port
      @plugin_port || Fabriq::PluginPort
    end

    def read_config_file
      @runtime_env = ENV['FABRIQ_ENV']
      fail_if_configuration_file_not_found
      self.plugin_port.require_plugins

      debug "Configuration: #{configuration_file_path}"
      load(configuration_file_path)
    end

    def fail_if_configuration_file_not_found
      unless configuration_file_available?
        raise RuntimeError.new("Configuration file config.rb not found")
      end
    end

    def configuration_file_available?
      configuration_file_path.kind_of?(String)
    end

    def configuration_file_path
      [File.expand_path("~") + "/.fabriq/config.rb", File.join(Dir.pwd, "config.rb")].each do |path|
        return path if File.exist?(path)
      end
      nil
    end

    def register_plugin(klass, &config_block)
      self.plugin_port.register_plugin(klass, &config_block)
    end

  end
end